import FeedbackDtlApi from '../../../../public/js/common/api/ctn/feedbackdetials.api.js'
import doT from '../../../vendor/template/doT.js';
(function (window, document, $) {
    'use strict';
    let _vm, feekbacklist = [], totalPages, catetype = 1;
    let _queryJson = {
        "typeEnum": "",
        "searchInfo": "",
        "startTime": "",
        "endTime": "",
        "sort": "feedbackTime desc",
        "pageIndex": 0,
        "pageSize": 10
    }
    let _template = doT.template($('#cgList-template').text());
    $('body').attr('data-weui-theme', 'light'); // 强制光明模式

    $(function () {
        _vm = new Vue({
            el: "#categorylist",
            data() {
                return {
                    edi: '',
                    queryJson: { ..._queryJson },
                }
            },
            methods: {
                truckLoadingTime: function (param) {
                    // 保存当前的滚动距离
                    scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                    // 设置 fixed 布局
                    document.body.style.position = 'fixed';
                    document.body.style.height = '100%';
                    document.body.style.width = '100%';
                }
            }
        });
        _initCommand()
        pullLoad();
        _initSlide()

    });
    // 命令初始化
    const _initCommand = () => {
        FeedbackDtlApi.getLoginUserToStaff().then(res => {
            _vm.edi = res.id
            _initDate()
        })
        // 导航栏
        $('#categorylist-navbar a').on('click', function () {
            $(this).addClass('weui-bar__item--on').siblings().removeClass('weui-bar__item--on');
        })
        $('#proposal-list').on('click', function () {
            catetype = 1
            _vm.queryJson.pageIndex = 0
            $('#ctlist-main').empty()
            feekbacklist = []
            _initDate()
        })
        $('#complaint-list').on('click', function () {
            catetype = 2
            _vm.queryJson.pageIndex = 0
            $('#ctlist-main').empty()
            feekbacklist = []
            _initcomDate()
        })
        $(document).on('click', '.lookcate', function () {
            // let index = $(this).parents('.lookcate').index()
            let index = $(this).index()
            let qureyid = feekbacklist[index].id
            window.location.href = `./cgdetails.html?v=${new Date().getTime()}&id=${qureyid}&type=${catetype}`;
        })
    }
    const _initDate = () => {
        FeedbackDtlApi.ppsReceiveList(_vm.edi, _vm.queryJson).then(res => {
            totalPages = res.totalPages
            _vm.queryJson.pageIndex = Number(_vm.queryJson.pageIndex) + 1
            feekbacklist.push(...res.items)
            $('#ctlist-main').append(_template(res.items));
        })
    }
    const _initcomDate = () => {
        FeedbackDtlApi.comReceiveList(_vm.edi, _vm.queryJson).then(res => {
            totalPages = res.totalPages
            _vm.queryJson.pageIndex = Number(_vm.queryJson.pageIndex) + 1
            feekbacklist.push(...res.items)
            $('#ctlist-main').append(_template(res.items));
        })
    }
    //TODO : 上拉加载
    const pullLoad = () => {
        // 监听滚动的父容器
        $("body").infinite().on("infinite", function () {
            if (_vm.queryJson.pageIndex == totalPages) return;
            let self = this;
            if (self.loading) return;
            self.loading = true;
            setTimeout(function () {
                if (catetype === 1) {
                    _initDate();
                }
                else {
                    _initcomDate()
                }
                self.loading = false;
            }, 500);   //模拟延迟
        });
    }
    // 下拉加载
    const _initSlide = () => {
        //分别设置滑动距离，开始位置，结束位置，和模拟数据的定时器
        let disY, startY, endY, timer, t, pullheight;
        // 触摸开始
        $('#categorylist').on('touchstart', function (e) {
            startY = e.originalEvent.changedTouches[0].pageY;//开始时的坐标
        });
        // 移动中
        $('#categorylist').on('touchmove', function (e) {
            endY = e.originalEvent.changedTouches[0].pageY;
            disY = endY - startY;//移动后的坐标减去开始坐标，滑动距离。
            if (isTop() && disY > 0) {
                e.preventDefault();
            }
            if (disY < 60) {
                $('#pull-down').css({
                    height: disY + 'px'//父盒子的高度随着滑动距离变化，有最大值。
                });
            } else {
                $('#pull-down').css({
                    height: 60 + 'px'//父盒子的最大高度
                });
            }
            // 结束时
            $('#categorylist').on('touchend', function (e) {
                clearInterval(timer);
                endY = e.originalEvent.changedTouches[0].pageY;
                disY = endY - startY;
                if (isTop() && disY > 0) {
                    timer = setInterval(function () {
                        $('#pull-down').css({
                            height: 0 + 'px',//松手后的返回。
                        });
                        clearInterval(timer);
                        _vm.queryJson.pageIndex = 0
                        $('#ctlist-main').empty()
                        feekbacklist = []
                        if (catetype === 1) {
                            _initDate();
                        }
                        else {
                            _initcomDate()
                        }
                    }, 300)
                } else {
                    $('#pull-down').css({
                        height: 0 + 'px',//松手后的返回。
                    });
                }
                $('#categorylist').off('touchend')
            })
        });
    }
    const isTop = () => {
        var t = document.documentElement.scrollTop || document.body.scrollTop;
        return t === 0 ? true : false;
    }
})(window, document, jQuery)